package com.sumainfor.common.util;

import com.alibaba.fastjson.JSONObject;
import com.sumainfor.common.entity.MessageEntity;
import org.apache.commons.io.IOUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;

/**
 * @Author EddyChen
 * @Email 835033913@qq.com
 * @Create 2019/6/28 11:16
 */
public class SystemUtils {


    /**
     * 获取方法名
     *
     * @return
     */
    public static String getCurrentMethodName() {
        int level = 1;
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        String methodName = stacks[level].getMethodName();
        return methodName;
    }


    /**
     * 获取类名
     *
     * @return
     */
    public static String getCurrentClassName() {
        int level = 1;
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        String className = stacks[level].getClassName();
        return className;
    }


    /**
     * 获取行号
     *
     * @return
     */
    public static int getLineNumber() {
        int level = 1;
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        int lineNumber = stacks[level].getLineNumber();
        return lineNumber;
    }


    /**
     * 获取错误的打印信息
     *
     * @param e
     * @return
     */
    public static String getErrorStackTrace(Throwable e) {
        StringWriter writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        String message;
        try {
            e.printStackTrace(printWriter);
            message = writer.toString();
        } finally {
            IOUtils.closeQuietly(printWriter);
            IOUtils.closeQuietly(writer);
        }
        return message;
    }

    /**
     * 构建一个错误消息
     *
     * @param title
     * @param serverName
     * @param e
     * @return
     */
    public static String createMessage(String title, Integer code, String serverName, Throwable e) {
        MessageEntity entity = new MessageEntity();
        entity.setErrorCode(code);
        entity.setErrorTitle(title);
        entity.setErrorMessage(getErrorStackTrace(e));
        entity.setServerName(serverName);
        entity.setCreateTime(new Date());
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
            if (addr != null) {
                entity.setHost(addr.getHostAddress() + ":" + addr.getHostName());
            }
        } catch (UnknownHostException ex) {

        }
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (servletRequestAttributes != null) {
            HttpServletRequest request = servletRequestAttributes.getRequest();
            if (request != null) {
                entity.setRequestUrl(request.getServletPath());
                entity.setRequestParams(JSONObject.toJSONString(request.getParameterMap()));
            }
        }
        return JSONObject.toJSONString(entity);
    }

}
